/**
 * Created by DELL on 2018/9/21.
 */
(function () {
  window.PlayWork = function () {
    this.bgAudio = null;         // 背景音乐audio
    this.playData = [];           // 播放数据
    this.playing_audio_index = 0;      // 播放中 第几个音频this.文件
    this.workAudios = [];
    this.playEndFun = null;           // 播放结束的回调
    this.audioSource1_dom = [];
    this.audioSource2_dom = [];
    this.audioSource3_dom = [];
    this.audioSource4_dom = [];
    this.initAudio_play = false;  // 所以音频文件需要
    this.timer = null;                 // 定时器
    this.createAudios_dom();
  };
  PlayWork.prototype = {
    init: function (music_id, playData, playEndFun) {
      this.playEndFun = playEndFun;
      this.playData = playData;
      this.playing_audio_index = 0;
      this.initPlayWorkAudios(music_id, playData);
    },
    // 创建所有音频文件
    createAudios_dom: function () {
      var audioSource1_dom = [];
      var audioSource2_dom = [];
      var audioSource3_dom = [];
      var audioSource4_dom = [];
      for (var j = 0; j < audioSource1.length; j++) {
        var audio_src = audioSource1[j].src;
        // var audioItem = new Audio(audio_src);
        // audioItem.preload = 'auto';
        var audioItem = Hilo.WebSound.getAudio({
          src: audio_src,
          loop: false,
          volume: 1
        });
        audioItem.load();
        audioSource1_dom.push(audioItem);
      }
      for (var i = 0; i < audioSource2.length; i++) {
        var audio_src = audioSource2[i].src;

        // var audioItem = new Audio(audio_src);
        // audioItem.preload = 'auto';

        var audioItem = Hilo.WebSound.getAudio({
          src: audio_src,
          loop: false,
          volume: 1
        });
        audioItem.load();
        audioSource2_dom.push(audioItem);
      }
      for (var k = 0; k < audioSource3.length; k++) {
        var audio_src = audioSource3[k].src;
        // var audioItem = new Audio(audio_src);
        // audioItem.preload = 'auto';
        var audioItem = Hilo.WebSound.getAudio({
          src: audio_src,
          loop: false,
          volume: 1
        });
        audioItem.load();
        audioSource3_dom.push(audioItem);
      }
      for (var g = 0; g < audioSource4.length; g++) {
        var audio_src = audioSource4[g].src;
        // var audioItem = new Audio(audio_src);
        // audioItem.preload = 'auto';
        var audioItem = Hilo.WebSound.getAudio({
          src: audio_src,
          loop: false,
          volume: 1
        });
        audioItem.load();
        audioSource4_dom.push(audioItem);
      }
      this.audioSource1_dom = audioSource1_dom;
      this.audioSource2_dom = audioSource2_dom;
      this.audioSource3_dom = audioSource3_dom;
      this.audioSource4_dom = audioSource4_dom;

    },
    // 初始化所有音频文件
    initPlayWorkAudios: function (music_id, playData) {
      var origin_music = {};
      this.playData = playData || [];
      for (var i = 0; i < music_config.length; i++) {
        var item = music_config[i];
        if (music_id / 1 === item.id) {
          origin_music = item;
          break;
        }
      }
      if (!item.id) {
        console.log('作品原id不存在');
        return false
      }
      var bg_music = item.bg_music;

      // 初始化bg音乐
      // this.bgAudio = Hilo.WebSound.getAudio({
      //   src: bg_music,
      //   loop: true,
      //   volume: 1
      // });
      this.bgAudio = new Audio(bg_music);
      this.bgAudio.load();
      this.bgAudio.loop = 'loop';
    },
    // 创建用户作品的audio 每个音频对应一个audio 太卡顿
    createWorkAudios: function () {
      var playData = this.playData;
      var workAudios = [];
      for (var j = 0; j < playData.length; j++) {
        var playDataItem = playData[j];
        var audioSource = audioSource2; // 中音
        if (playDataItem.tone === 1) { // 低音
          audioSource = audioSource1;
        } else if (playDataItem.tone === 3) { // 高音
          audioSource = audioSource3;
        } else if (playDataItem.tone === 4) {
          audioSource = audioSource4;
        }
        audio_src = audioSource[playDataItem.note - 1].src;
        var audioItem = new Audio(audio_src);
        audioItem.preload = 'auto';
        workAudios.push(audioItem);
      }
      this.workAudios = workAudios;
    },
    // 开始播放音频文件
    start_play: function () {
      // 激活音频功能。注意：需用户事件触发此方法才有效。目前仅对WebAudio有效。
      this.bgAudio.play();
      Hilo.WebSound.enableAudio();
      this.playAudioItem2();

      // var _this = this;
      // var interval = setInterval(checkCanPlay, 100);
      // //
      // function checkCanPlay() {
      //   if (_this.bgAudio.playing) {
      //     clearInterval(interval);
      //     _this.playAudioItem2();
      //   } else {
      //     _this.bgAudio.play();
      //   }
      // }
    },
    // 播放单一文件 每个音频文件对应一个音符
    playAudioItem: function () {
      var _this = this;
      var workAudios = this.workAudios;
      var index = this.playing_audio_index;
      var current_audio = workAudios[index];
      var playDataItem = this.playData[index];

      var last_start_time = 0;
      if (index > 0) {
        last_start_time = this.playData[index - 1].start_time;
      }
      if (this.playing_audio_index === workAudios.length) {
        this.stop_playMusic()
      } else {
        var next_time = playDataItem.start_time - last_start_time;

        this.playing_audio_index = this.playing_audio_index + 1;
        setTimeout(function () {
          current_audio.play();
          _this.playAudioItem();
        }, next_time);
      }

    },
    // ios 音频需要初始化播放一次
    initAudioPlay_first: function () {
      if (!this.initAudio_play) {
        this.initAudio_play = true;
        var audios = this.audioSource1_dom.concat(this.audioSource2_dom, this.audioSource3_dom, this.audioSource4_dom);
        for (var i = 0; i < audios.length; i++) {
          var audio_item = audios[i];

          audio_item.muted = true;
          audio_item.play();
          this.audio_reset(audio_item);
        }
      }
    },
    audio_reset: function (audio_item) {
      setTimeout(function () {
        audio_item.pause();
        audio_item.currentTime = 0;
        audio_item.muted = false;
      }, 0);
    },

    // 播放单一文件 相同音频使用同一个音频文件
    playAudioItem2: function () {
      var _this = this;
      var index = this.playing_audio_index;
      var playData = this.playData;


      var last_start_time = 0;
      if (index > 0) {
        last_start_time = this.playData[index - 1].start_time;
      }
      if (this.playing_audio_index === playData.length) {
        setTimeout(function () {
          _this.playMusic_end()
        }, 2000)
      } else {
        var playDataItem = playData[index];
        var audioSource_dom = this.audioSource2_dom; // 中音
        if (playDataItem.tone === 1) { // 低音
          audioSource_dom = this.audioSource1_dom;
        } else if (playDataItem.tone === 3) { // 高音
          audioSource_dom = this.audioSource3_dom;
        } else if (playDataItem.tone === 4) {
          audioSource_dom = this.audioSource4_dom;
        }


        var music_type = playDataItem.note;
        var current_audio = audioSource_dom[music_type - 1];

        var next_time = playDataItem.start_time - last_start_time;

        this.playing_audio_index = this.playing_audio_index + 1;
        this.timer = setTimeout(function () {
          // current_audio.play();
          // if (current_audio.playing) {
          //   setTimeout(function () {
          //     current_audio.pause();
          //     current_audio.currentTime = 0;
          //     current_audio.play();
          //   }, 0);
          // } else {
          //
          // }
          current_audio.play();
          _this.playAudioItem2();
        }, next_time);
      }
    },
    // 播放结束
    playMusic_end: function () {
      this.bgAudio.pause();
      this.bgAudio.currentTime = 0;
      this.playing_audio_index = 0;
      if (this.playEndFun) {
        this.playEndFun()
      }
    },
    // 暂停播放
    stop_play: function () {
      clearTimeout(this.timer);
      if (this.bgAudio) {
        this.bgAudio.pause();
      }

    }

  }
})();